//pwm.c
#include "pwm.h"

void PWM_TIM3_CH1_Init(void)
{
	GPIO_InitTypeDef GPIO_Config;
	TIM_TimeBaseInitTypeDef TIM_Config;
	TIM_OCInitTypeDef TIM_OC_Config;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	
	GPIO_Config.GPIO_Pin = GPIO_Pin_6;
	GPIO_Config.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_Config.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_Config);
	
	TIM_Config.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_Config.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_Config.TIM_Period = 100 - 1;
	TIM_Config.TIM_Prescaler = 720 -1;
	TIM_Config.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(TIM3,&TIM_Config);
	
	TIM_OCStructInit(&TIM_OC_Config);
	TIM_OC_Config.TIM_OCMode = TIM_OCMode_PWM1;
	TIM_OC_Config.TIM_OCPolarity = TIM_OCPolarity_High;
	TIM_OC_Config.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OC_Config.TIM_Pulse = 0;
	TIM_OC1Init(TIM3,&TIM_OC_Config);
	
	TIM_Cmd(TIM3,ENABLE);
}

void PWM_TIM4_CH1_Init(void)
{
	GPIO_InitTypeDef GPIO_Config;
	TIM_TimeBaseInitTypeDef TIM_Config;
	TIM_OCInitTypeDef TIM_OC_Config;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
	
	GPIO_Config.GPIO_Pin = GPIO_Pin_6;
	GPIO_Config.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_Config.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_Config);
	
	TIM_Config.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_Config.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_Config.TIM_Period = 100 - 1;
	TIM_Config.TIM_Prescaler = 720 -1;
	TIM_Config.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(TIM4,&TIM_Config);
	
	TIM_OCStructInit(&TIM_OC_Config);
	TIM_OC_Config.TIM_OCMode = TIM_OCMode_PWM1;
	TIM_OC_Config.TIM_OCPolarity = TIM_OCPolarity_High;
	TIM_OC_Config.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OC_Config.TIM_Pulse = 0;
	TIM_OC1Init(TIM4,&TIM_OC_Config);
	
	TIM_Cmd(TIM4,ENABLE);
}

void PWM_SetCompare1(TIM_TypeDef* TIMx,u16 Compare)
{
	TIM_SetCompare1(TIMx,Compare);
}




